package com.hckj.jianzhijia;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.hckj.jianzhijia.interfaces.UserInfoChangeInterface;
import com.hckj.jianzhijia.model.City;
import com.hckj.jianzhijia.model.CompanyUser;
import com.hckj.jianzhijia.model.Province;
import com.hckj.jianzhijia.model.Region;
import com.hckj.jianzhijia.networks.GetCityNetWork;
import com.hckj.jianzhijia.networks.GetProvinceNetWork;
import com.hckj.jianzhijia.networks.GetRegionNetWork;
import com.hckj.jianzhijia.popupwindows.PublishPopupWindowCity;
import com.hckj.jianzhijia.popupwindows.PublishPopupWindowProvince;
import com.hckj.jianzhijia.popupwindows.PublishPopupWindowRegion;
import com.hckj.jianzhijia.utils.ToastUtils;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class ChangeAddressActivity extends BaseActivity implements UserInfoChangeInterface, PublishPopupWindowCity.PublishPopWinCityInterface,
                                                                               PublishPopupWindowProvince.PublishPopWinProvinceInterface,
                                                                               PublishPopupWindowRegion.PublishPopWinRegionInterface,
                                                                               GetCityNetWork.NetWorkInterface,
                                                                               GetProvinceNetWork.NetWorkInterface,
                                                                               GetRegionNetWork.NetWorkInterface {

    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.tv_choose_province)
    TextView tvChooseProvince;
    @InjectView(R.id.ll_choose_province)
    LinearLayout llChooseProvince;
    @InjectView(R.id.tv_choose_city)
    TextView tvChooseCity;
    @InjectView(R.id.ll_choose_city)
    LinearLayout llChooseCity;
    @InjectView(R.id.tv_choose_region)
    TextView tvChooseRegion;
    @InjectView(R.id.ll_choose_area)
    LinearLayout llChooseArea;
    @InjectView(R.id.edt_address)
    EditText edtAddress;
    @InjectView(R.id.btn_commit)
    Button btnCommit;
    @InjectView(R.id.ll_parent)
    LinearLayout llParent;

    private int cityId = -1;
    private int provinceId = -1;
    private int regionId = -1;
    private PublishPopupWindowCity publishPopupWindowCity;
    private PublishPopupWindowProvince publishPopupWindowProvince;
    private PublishPopupWindowRegion publishPopupWindowRegion;

    private ProgressDialog loadingDialog;
    private GetProvinceNetWork getProvinceNetWork;
    private GetCityNetWork getCityNetWork;
    private GetRegionNetWork getRegionNetWork;
    private boolean netWorIsOn = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_address);
        ButterKnife.inject(this);
        ((JianzhijiaApplacation) getApplication()).addUserInfoChange(this);
        loadingDialog = new ProgressDialog(this);
        loadingDialog.setCancelable(false);
        loadingDialog.setMessage("正在上传");

        initData();
        initEvent();
    }

    private void initData() {
        CompanyUser companyUser = jianzhijiaApplacation.getCompanyUser();
        edtAddress.setText(companyUser.getCompanyAddress());
    }

    private void initEvent() {

        tvChooseProvince.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(netWorIsOn){
                    return;
                }
                cityId = -1;
                regionId = -1;
                tvChooseCity.setText("选择国别");
                tvChooseRegion.setText("选择地区");
                netWorIsOn = true;
                getProvinceNetWork = new GetProvinceNetWork(ChangeAddressActivity.this);
                getProvinceNetWork.execute();
            }
        });

        tvChooseCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(netWorIsOn){
                    return;
                }
                regionId = -1;
                tvChooseRegion.setText("选择地区");
                if (provinceId == -1) {
                    ToastUtils.showToast(getApplicationContext(), "请选择省份");
                    return;
                } else {
                    netWorIsOn = true;
                    getCityNetWork = new GetCityNetWork(provinceId, ChangeAddressActivity.this);
                    getCityNetWork.execute();
                }
            }
        });

        tvChooseRegion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(netWorIsOn){
                    return;
                }

                if (provinceId == -1) {
                    ToastUtils.showToast(getApplicationContext(), "请选择省份");
                    return;
                } else if (cityId == -1) {
                    ToastUtils.showToast(getApplicationContext(), "请选择城市");
                    return;
                } else {
                    netWorIsOn = true;
                    getRegionNetWork = new GetRegionNetWork(cityId, ChangeAddressActivity.this);
                    getRegionNetWork.execute();
                }
            }
        });



        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(provinceId == -1){
                    ToastUtils.showToast(getApplicationContext(),"请选择省份");
                    return;
                }else if(cityId == -1){
                    ToastUtils.showToast(getApplicationContext(),"请选择城市");
                    return;
                }else if(regionId == -1){
                    ToastUtils.showToast(getApplicationContext(), "请选择城区");
                    return;
                }else if(edtAddress.getText() == null ||edtAddress.getText().toString().equals("")){
                    ToastUtils.showToast(getApplicationContext(), "请输入详细位置");
                    return;
                }

                loadingDialog.show();
                jianzhijiaApplacation.getCompanyUser().setCityId(cityId);
                jianzhijiaApplacation.getCompanyUser().setRegionId(regionId);
                jianzhijiaApplacation.getCompanyUser().setProvinceId(provinceId);
                jianzhijiaApplacation.getCompanyUser().setCompanyAddress(edtAddress.getText().toString());
                jianzhijiaApplacation.getCompanyUser().setProvinceName(tvChooseProvince.getText().toString());
                jianzhijiaApplacation.getCompanyUser().setCityName(tvChooseCity.getText().toString());
                jianzhijiaApplacation.getCompanyUser().setRegionName(tvChooseRegion.getText().toString());
                jianzhijiaApplacation.getCompanyUser().saveUserInfo().saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            loadingDialog.dismiss();
                            ToastUtils.showToast(getApplicationContext(), "修改成功");
                            jianzhijiaApplacation.updateCompanyInfo();
                            finish();
                        } else {
                            loadingDialog.dismiss();
                            ToastUtils.showToast(getApplicationContext(), "修改失败");
                        }
                    }
                });
            }
        });
    }


    @Override
    public void setCity(int position, String itemName) {
        this.cityId = position;
        tvChooseCity.setText(itemName);
    }

    @Override
    public void setProvince(int position, String itemName) {
        this.provinceId = position;
        tvChooseProvince.setText(itemName);
    }

    @Override
    public void setRegion(int position, String itemName) {
        this.regionId = position;
        tvChooseRegion.setText(itemName);
    }

    @Override
    public void updateCompanyInfo() {

    }

    @Override
    public void updateCompanyLogo() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ((JianzhijiaApplacation) getApplication()).removeUserInfoChangeInterface(this);
    }

    @Override
    public void afterGetProvinces(ArrayList<Province> provinces) {
        netWorIsOn = false;
        if(provinces==null){
            ToastUtils.showToast(getApplicationContext(),"网络错误，无法获取省份列表");
            return;
        }else if(provinces.size() == 0){
            ToastUtils.showToast(getApplicationContext(),"省份列表为空");
            return;
        }

        publishPopupWindowProvince = PublishPopupWindowProvince.newInstance(this, this, llParent, provinces);
        publishPopupWindowProvince.showAtLocation(llParent, Gravity.CENTER, 0, 0);
    }

    @Override
    public void afterGetCities(ArrayList<City> cities) {
        netWorIsOn = false;
        if(cities==null){
            ToastUtils.showToast(getApplicationContext(),"网络错误，无法获取城市列表");
            return;
        }else if(cities.size() == 0){
            ToastUtils.showToast(getApplicationContext(),"城市列表为空");
            return;
        }

        publishPopupWindowCity = PublishPopupWindowCity.newInstance(this, this, llParent, cities);
        publishPopupWindowCity.showAtLocation(llParent, Gravity.CENTER, 0, 0);
    }

    @Override
    public void afterGetRegions(ArrayList<Region> regions) {
        netWorIsOn = false;
        if(regions==null){
            ToastUtils.showToast(getApplicationContext(),"网络错误，无法获取区县列表");
            return;
        }else if(regions.size() == 0){
            ToastUtils.showToast(getApplicationContext(),"区县列表为空");
            return;
        }

        publishPopupWindowRegion = PublishPopupWindowRegion.newInstance(this, this, llParent, regions);
        publishPopupWindowRegion.showAtLocation(llParent, Gravity.CENTER, 0, 0);
    }

}
